//
//  VisitorLoginView.swift
//  Weibo19
//
//  Created by 城 on 15/11/8.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit


@objc protocol VisitorLoginViewDelegate: NSObjectProtocol {
    //协议方法
    //登陆
    func visitorWillLogin()
    //注册
   optional func visitorWillRegister()
}

class VisitorLoginView: UIView {
    weak var visitorLoginVDelegate: VisitorLoginViewDelegate?
    
    @objc private  func loginDidBtnClick(){
    visitorLoginVDelegate?.visitorWillLogin()
    
    }
    @objc private func registerBtnDidClick(){
     visitorLoginVDelegate?.visitorWillRegister? ()
    }
    //设置访客图片信息,每一个界面都能获取这个方法
    func setUIInfo(imageNmae:String?,titleLabel:String){
        tipLabel.text = titleLabel
        iconView.hidden = false
        if imageNmae != nil{
            circleView.image = UIImage(named: imageNmae!)
            bringSubviewToFront(circleView)
            iconView.hidden = true
            
        }else{
            startanimation()

        }
    
           }
    
    func startanimation(){
    
        let animation = CABasicAnimation()
        animation.keyPath = "transform.rotation"
        animation.toValue = 2 * M_PI
        animation.duration = 20
        animation.repeatCount = MAXFLOAT
        animation.removedOnCompletion = false
        circleView.layer .addAnimation(animation, forKey: "key")

    }
    
    //调用构照方法
    override init(frame:CGRect) {
        super.init(frame: CGRectZero)
    
     stopUI()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func stopUI(){
        //添加子控件
 
         addSubview(circleView)//圆圈
        addSubview(blakImage)//蒙版图片
               addSubview(iconView)//房子
        addSubview(tipLabel)//文本
        addSubview(loginBtn)//登录按钮
        addSubview(registerBtn)//注册按钮
        

    
        
        for v in subviews{
            //关闭frame的自动布局
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        
             //VFL语言自动布局
        
        //布局圆圈 设置中心对齐,参照物是本身
       addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1.0, constant: 0))

        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterY, relatedBy: .Equal, toItem: self,  attribute: .CenterY, multiplier: 1.0, constant: -60))
        //布局房子 设置中心对齐,参照物是circleView 中心对齐
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1.0, constant: 0))

        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterY, relatedBy: .Equal, toItem: circleView, attribute: .CenterY, multiplier: 1.0, constant: 0))
        //布局文本信息,,参照物是circleView 设置中心对齐,,距上面有16的距离
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Top, relatedBy: .Equal, toItem: circleView, attribute: .Bottom, multiplier: 1.0, constant: 30))
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 224))
          addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 50))
        
        //布局登录按钮
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Left, relatedBy:.Equal , toItem: tipLabel, attribute: .Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Top, relatedBy: .Equal, toItem: tipLabel, attribute: .Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 35))
        //布局注册按钮
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Right, relatedBy:.Equal , toItem: tipLabel, attribute: .Right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Top, relatedBy: .Equal, toItem: tipLabel, attribute: .Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 35))
       
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[blakImage]-0-|", options: [], metrics: nil, views: ["blakImage":blakImage]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[blakImage]-(-35)-[loginBtn]", options: [], metrics: nil, views: ["blakImage":blakImage,"loginBtn":loginBtn]))
        
        backgroundColor = UIColor(white: 0.93, alpha: 1)
        
    }
    //懒加载
    //设置圆圈的图片
    lazy var circleView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //设置房子的图片
    lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //设置蒙版图片
    lazy var blakImage:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    //设置文本信息
    lazy var tipLabel:  UILabel = {
        //实例化
        let l = UILabel()
        //文本信息
        l.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        //设置字体对齐
        l.textAlignment = NSTextAlignment.Center
        //字体大小
        l.font = UIFont.systemFontOfSize(14)
        //字体颜色
        l.textColor = UIColor.lightGrayColor()
        //换行
        l.numberOfLines = 0
        //大小
        l.sizeToFit()
        return l
    }()
    
    //设置登录按钮
    lazy var loginBtn: UIButton = {
    //实例化
     let btn = UIButton()
    //设置登录字体
   btn.setTitle("登录", forState: .Normal)
        //设置登录图片
  btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        //设置亮灰色
   btn.setTitleColor(UIColor.lightGrayColor(), forState: .Normal)
 
        btn.addTarget(self, action: "loginDidBtnClick", forControlEvents: .TouchUpInside)
    return btn
    }()
    //设置注册按钮
    lazy var registerBtn: UIButton = {
      //实例化
        let reg = UIButton()
    //设置注册字体
        reg.setTitle("注册", forState: .Normal)
        //设置注册图片
        reg.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        //设置橙色
        reg.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        reg.addTarget(self, action: "registerBtnDidClick", forControlEvents: .TouchUpInside)
    return reg
    }()
    
    
    
}
